package cn.edu.swu.user;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("user");
        String password = request.getParameter("password");

        User admin = new User();
        admin.setName("管理员");
        admin.setUser(userName);
        admin.setPassword(password);
        try {
            UserRepo.getInstance().register(admin);
        } catch (SQLException e) {
            response.getWriter().println("<html>\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <title>My Java Web APP</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "<a href=\"login.html\">"+e.getMessage()+"</a>\n" +
                    "</body>\n" +
                    "</html>");
        }
        response.getWriter().println("<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>My Java Web APP</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<a href=\"login.html\">注册成功点击跳转登录页面</a>\n" +
                "</body>\n" +
                "</html>");

    }
}
